<?php

namespace addons\Patrol\common\models;

use Yii;
use common\helpers\ArrayHelper;
use common\enums\StatusEnum;

/**
 * This is the model class for table "{{%addon_oa_calendar}}".
 *
 * @property int $id ID
 * @property int $member_id 用户
 * @property string $title 主题
 * @property string $begin_date 开始日期
 * @property string $end_date 结束日期
 * @property int $remind 提醒时间
 * @property int $type 日程类型
 * @property string $attend 与会人员
 * @property int $address 地点
 * @property int $content 描述
 * @property int $repetition 1重复0不重复
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class PatrolClasses extends \common\models\base\BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_patrol_classes}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title', 'begin_time', 'end_time'], 'required'],
            [['merchant_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '班次',
            'begin_time' => '开始时间',
            'end_time' => '结束时间',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public static function getDropDown()
    {
        $models = PatrolClasses::find()
            ->select(['id', 'title'])
            ->andWhere(['status'=>StatusEnum::ENABLED])
            ->andFilterWhere(['merchant_id' => Yii::$app->user->identity->merchant_id])
            ->orderBy('id asc,title asc')
            ->asArray()
            ->all();
        return ArrayHelper::map($models, 'id', 'title');
    }

    public function beforeSave($insert)
    {
        $this->merchant_id = Yii::$app->user->identity->merchant_id;

        return parent::beforeSave($insert);

    }
}
